<!--
 * @Author: 夏之一周
 * @Date: 2024-05-09 11:09
 * @Description: 
 * @LastEditors: 夏之一周 <https://wudetian.top>
 * @LastEditTime: 2024-05-09 16:05
-->
<template>
  <div id="adimin">
    <ul>
      <li v-for="i in this.userdata" :key="i.name">
        <van-field v-model="i.password" center :readonly="!i.change" clearable :label="i.name" placeholder="请设置用户密码">
          <template #button>
            <van-button size="small" type="danger" @click="remove(i)"> 删除 </van-button>
            <van-button size="small" type="primary" @click="setpassword(i.name, i.password)"> 修改 </van-button>
          </template>
        </van-field>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "Adimin",
  beforeCreate() {
    console.log(this.$route.params.userdata);
    this.userdata = this.$route.params.userdata;
  },
  methods: {
    remove(i) {
      // 删除用户信息文件  重写用户列表文件
      var index = this.userdata.indexOf(i);
      this.userdata.splice(index, 1);
      console.log(i.name);
      this.$forceUpdate();
      this.$axios({
        // 请求方法
        method: "POST",
        // 请求url
        url: "http://8.142.208.202:8003/chang",
        data: this.userdata,
      }).catch((err) => {
        // 打印错误信息
        console.error(err);
      });
      this.$axios({
        // 请求方法
        method: "POST",
        // 请求url
        url: "http://8.142.208.202:8003/remove",
        data: [i.name],
      })
        .then((response) => {
          // 处理请求的结果
          if (response.data == "ok") {
            // 显示新的弹出层 - 清空密码区域
            alert("删除成功");
            return;
          }
        })
        .catch((err) => {
          // 打印错误信息
          console.error(err);
        });
    },
    setpassword(name, psw) {
      this.$router.push({
        name: "mes",
        params: {
          name: name,
          password: psw,
        },
      });
    },
  },
  mounted() {
    this.$axios({
      method: "POST",
      url: "http://8.142.208.202:8003/data",
      data: {
        name: this.$route.params.userdata.name,
        password: this.$route.params.userdata.password,
      },
    }).then((res) => {
      console.log(res.data);
    });
  },
};
</script>

<style scoped>
ul {
  width: 100vw;
}
</style>
